package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class UpdateDemo {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/java76?useSSL=false";
        Connection con = null;
        Statement st = null;
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入课程名：");
        String newName=sc.nextLine();
        //newName=newName.replaceAll("'","");
        System.out.println("新课时：");
        int hours=sc.nextInt();
        System.out.println("新年级号：");
        long gradeid=sc.nextLong();
        System.out.println("要改的课程编号：");
        long id=sc.nextLong();
        String sql="update s_subject set subjectname='"+newName+"',classhour="+hours+",gradeid="+gradeid+" where id="+id;
        System.out.println(sql);
        try {
            con = DriverManager.getConnection(url, "root", "root1234");
            //System.out.println(con.getClass().getName());
            st = con.createStatement();

            int r=st.executeUpdate(sql);
            System.out.println("修改了"+r+"条");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
